/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mongoosehome.web.onlinemall.product.dao;

import com.mongoosehome.web.common.domain.TProducts;
import com.mongoosehome.web.util.HibernateUtils;
import com.mongoosehome.web.util.PageInfo;
import com.mongoosehome.web.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Administrator
 */
public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {

    @Override
    public PageInfo getProductsList(int pageNo,int maxResult,String sortField,String sortDir) {     
        StringBuilder hql = new StringBuilder(" from TProducts a" );        
        if(StringUtils.isFine(sortField)){
            hql.append(" order by a.").append(sortField).append(" ").append(sortDir);
        }
	return HibernateUtils.getNumberPage(this.getSession(), this.getSessionFactory(), pageNo, maxResult, hql.toString(),null,false);
    }

    @Override
    public TProducts getProducts(int productId) {
        return (TProducts)this.getHibernateTemplate().get(TProducts.class, productId);
    }

    @Override
    public void saveProducts(TProducts tProducts) {
        this.getHibernateTemplate().save(tProducts);
    }
    
    @Override
    public boolean  isProducts(String name){
        List<String> args =  new ArrayList<String>();
        args.add(name);
        String hql = "from TProducts a where a.productName =?";
        int total = HibernateUtils.getFileCount(this.getSession(), this.getSessionFactory(), hql, args.toArray(), false);
        return total>0?true:false;
    }

    @Override
    public void updateProducts(TProducts tProducts) {
        this.getHibernateTemplate().update(tProducts);
    }
}
